package com.google.zxing.client.homework.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Base64;
import android.util.Log;

//import com.code.ui.MyApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.protocol.HTTP;

/**
 * http请求工具类
 * 官网文档说明：http://loopj.com/android-async-http/
 *
 * @author helijun
 */
public class NetHttpClient {
    static final String TAG = "NetHttpClient";
    private static AsyncHttpClient client = new AsyncHttpClient();

    private static void addHeader(){

    }

    /**
     * 参数为RequestParams的post请求
     * @param url
     * @param params
     * @param responseHandler
     */
    public static void post(String url, RequestParams params,
                            AsyncHttpResponseHandler responseHandler) {
        addRequestHeader();
//        client.addHeader("ticket", MyApplication.getInstance().getLoginToken());
        setPreParms();
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    /**
     * 参数为HttpEntity的post请求
     *
     * @param context
     * @param url
     * @param params
     * @param responseHandler
     */
    public static void post(Context context, String url, HttpEntity params,
                            AsyncHttpResponseHandler responseHandler) {
        addRequestHeader();
        setPreParms();
//        client.addHeader("ticket", MyApplication.getInstance().getLoginToken());
        client.post(context, getAbsoluteUrl(url), params, "application/json",
                responseHandler);
        Log.d(TAG,"post url==================="+getAbsoluteUrl(url));
    }

    /**
     * get 请求
     * @param context
     * @param url
     * @param responseHandler
     */
    public static void get(Context context, String url,
                           AsyncHttpResponseHandler responseHandler){
        addRequestHeader();
        setPreParms();
        client.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        client.get(context,getAbsoluteUrl(url),responseHandler);
    }

    /**
     * 上传文件
     *
     * @param context
     * @param url
     * @param params
     * @param responseHandler
     */
    public static void postFile(Context context, String url,
                                RequestParams params, AsyncHttpResponseHandler responseHandler) {
//        addRequestHeader();
//        setPreParms();
//        String BOUNDARY = UUID.randomUUID().toString();
//        client.addHeader("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
        client.post(context, getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return Constants.HTTP_BASE_URL + relativeUrl;
    }

    /**
     * 添加请求头
     */
    @SuppressLint("NewApi")
    private static void addRequestHeader() {
        client.addHeader("Authorization",
                Base64.encodeToString("".getBytes(), Base64.DEFAULT));
        client.addHeader("charset", HTTP.UTF_8);
    }

    /**
     * 设置一些http请求的一些参数，比如说超时时间
     */
    private static void setPreParms() {
        client.setTimeout(20 * 1000);
    }

    /**
     * 取消某个请求
     *
     * @param context
     */
    public static void cancel(Context context) {
        client.cancelRequests(context, true);
    }

}